package com.blb.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.springboot.entity.Goods;
import com.blb.springboot.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class GoodsController {
    Integer cur = 1;
    @Resource
    private GoodsService goodsService;

    @RequestMapping("/add")
    public String addGoods(Goods goods) {
        goodsService.save(goods);
        return "redirect:/all";
    }

    @RequestMapping("/del")
    public String delGoods(long id) {
        goodsService.removeById(id);
        return "redirect:/all";
    }

    @RequestMapping("/update")
    public String updateGoods(Goods goods) {
        goodsService.updateById(goods);
        return "redirect:/all";
    }

    @RequestMapping("/all")
    public String selectAllPages(HttpSession session,@RequestParam(value = "current",required = false,defaultValue = "") String current) {

        if (!"".equals(current)) {
            cur = Integer.parseInt(current);
        }
        if(cur<1){
            cur=1;
        }
        System.out.println(goodsService);
        IPage<Goods> goodsIPage = goodsService.selectAll(cur);
        List<Goods> records = goodsIPage.getRecords();
        long pages = goodsIPage.getPages();
        long total = goodsIPage.getTotal();

        session.setAttribute("records", records);
        session.setAttribute("total", total);
        session.setAttribute("pages", pages);
        session.setAttribute("cur", cur);
        return "List";
    }

}
